<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑模板</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript" src="../lib/layui/layui.all.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/jquery.min.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/commonutil.js"></script>
    <script src="./function.js" charset="utf-8"></script>
</head>
<body>
<div class="edit-form-class" style="padding-top: 20px;">
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form" id="editTemplateForm">
                <div class="layui-form-item">
                    <label class="layui-form-label">*模板编码</label>
                    <div class="layui-input-block">
                        <input type="text" id="templateNo" name="templateNo" readonly="readonly" style="width: 350px"
                               autocomplete="off" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*模板名称</label>
                    <div class="layui-input-block">
                        <input type="text" id="templateName" name="templateName"
                               lay-verify="required|templateNameVerify"
                               style="width: 350px" autocomplete="off" placeholder="" class="layui-input">
                        <input type="text" id="id" name="id" style="display:none;" autocomplete="off">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*模板来源</label>
                    <div class="layui-input-block">
                        <input type="text" id="tempSource" name="tempSource" style="display:none;" autocomplete="off">
                        <input type="radio" name="templateResource" lay-filter="templateListen" value="1" title="附件上传"
                               checked>
                        <input type="radio" name="templateResource" lay-filter="templateListen" value="2" title="线上编辑">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*模板文件</label>
                    <div class="layui-input-block" style="padding-top: 5px">
                        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-btn-radius"
                                id="uploadFile">
                            上传文件
                        </button>
                        <input id='templateExtId' name='templateExtId' type="text" class="text" maxlength='50'
                               style='display:none;'/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">*模板文件</label>
                    <div class="layui-input-block" style="padding-top: 5px">
                        <textarea id="templateEdit" name="templateEdit" style="display: none;"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">签章类型</label>
                    <div class="layui-input-block" style="width: 350px">
                        <select name="sealType" id="sealType" lay-filter="sealType"
                                lay-verify="required|sealTypeVerify">
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">公章规则</label>
                    <div class="layui-input-block" id="officialSealId" style="width: 350px">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" style="padding-left: 15px;">*配置审批流</label>
                    <div class="layui-input-block" id="approval"></div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block" style="width: 350px">
                        <textarea placeholder="请输入内容" class="layui-textarea" id="remark" name="remark"
                                  lay-verify="remarksVerify"></textarea>
                    </div>
                </div>
                <input id='version' name='version' type="text" class="text" maxlength='50' style='display:none;'/>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="padding-left: 80px;padding-top: 10px">
                        <button class="layui-btn" lay-submit="" autocomplete="off" lay-filter="submitBtn"
                                id="submitBtn">保存
                        </button>
                        <button id="closeWin" class="layui-btn layui-btn-primary">取消</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>

<script>
    jQuery(function () {
        layui.use(['laypage', 'jquery', 'layer', 'table', 'form', 'util', 'upload'], function () {
            let form = layui.form;
            let upload = layui.upload;
            let layedit = layui.layedit;

            //建立编辑器
            index = layedit.build('templateEdit');
            //隐藏文本编辑器
            hideLayEdit(form);
            //初始化渠道
            queryChannels(form);
            //初始化签章类型
            querySealType(form);
            //radio监听
            radioListener(form);
            //select监听
            selectListener2(form);
            //初始化数据
            buildEditWin(form, layedit, index);

            //文件上传
            let uploadUrl = serverPre + "/manager/scs/v2/template/upload";
            uploadFile(upload, uploadUrl);
            form.render();

            //监听textarea内容发送变化
            let id = GetUrlParam("id");
            let url = serverPre + "/manager/scs/v2/template/one/" + id;
            var data = requestGet(url);
            var value = data.fileData;
            $("iframe[textarea=templateEdit]").contents().find("body").keyup(function (e) {
                var tempVal = e.delegateTarget.innerText;
                if (value == tempVal) {
                    $("#approval").parent().hide();
                } else {
                    $("#approval").parent().show();
                }
                if (value == null && tempVal == "") {
                    $("#approval").parent().hide();
                }
            });
        });
    });

    //提交
    $('#submitBtn').click(function () {
        //判断审批流是否为空，为后台校验做准备
        var attr = $("#approval").parent().is(":hidden");
        var approverObj = approval.result();
        if (attr) {
            approverObj.approvers = null;
        }
        layui.use(['jquery', 'layedit'], function () {
            let layedit = layui.layedit;
            let fileData = layedit.getText(index);
            let url = serverPre + "/manager/scs/v2/template/update";
            let formData = serializeObject($("#editTemplateForm"));
            let officialSealId = [];
            $("#officialSealId").find('input:checkbox').each(function (i, item) {
                if ($(this).is(':checked')) {
                    officialSealId.push(item.value);
                }
            });
            //1.公章规则号校验
            if (formData.sealType !== "1") {
                if (officialSealId.length === 0) {
                    alert("公章规则号不能为空！");
                    return false;
                }
            }

            //2.如果模板来源发生变化，则应该判断是否为空.如果模板来源发生变化isChanged=true
            let id = GetUrlParam("id");
            let url2 = serverPre + "/manager/scs/v2/template/one/" + id;
            let data = requestGet(url2);
            let resourceVal = data.resource + "";
            let tempVal = $("#tempSource").val();
            if (resourceVal != tempVal) {
                switch (tempVal) {
                    case '1':
                        if (formData.templateExtId == null || formData.templateExtId == "") {
                            alert("上传文件不能为空！");
                            return false;
                        }
                        break;
                    case '2':
                        let fileData = layedit.getText(index);
                        if (fileData == null || fileData == "") {
                            alert("在线编辑内容不能为空！");
                            return false;
                        }
                        break;
                }
            }

            let commitData = JSON.stringify({
                "id": formData.id,
                "version": formData.version,
                "resource": formData.templateResource,
                "remark": formData.remark,
                "fileData": fileData,
                "templateNo": formData.templateNo,
                "templateName": formData.templateName,
                "refId": formData.templateExtId,
                "sealType": formData.sealType,
                "officialSealId": officialSealId,
                "userInfoRos": approverObj.userInfos,
                "approverIds": approverObj.approvers
            });
            submitForm(commitData, url);
        })
    });

    //取消
    $("#closeWin").click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    /**
     * 配置审批流
     */
    $(function () {
        $("#approval").load("../approval/approvalConfig.html", "", function () {
            approval = initSubmitApproval("开始");
        })
    });

</script>
</html>
